package com.yuwei.module_parking.view

import android.app.AlertDialog
import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.entity.ParkingEntity
import com.bw.lib_common.entity.ParkingEntityItem
import com.bw.lib_common.view.BaseMvvmActivity
import com.yuwei.module_parking.R
import com.yuwei.module_parking.adapter.ParkingAdapter
import com.yuwei.module_parking.databinding.ActivityParkingBinding
import com.yuwei.module_parking.vm.ParkingViewModel

@Route(path = RouterPath.MODULE_PARKING_ACTIVITY)
class ParkingActivity : BaseMvvmActivity<ActivityParkingBinding>() {
    private val vm:ParkingViewModel by viewModels()
    val parkingAdapter = ParkingAdapter()
    val parkingData = mutableListOf<ParkingEntityItem>()
    override fun initViews() {
        binding.parkingItemRv.adapter = parkingAdapter
        binding.parkingItemRv.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        vm.ParkingManage(2)
        binding.parkingAdd.setOnClickListener {
            ARouter.getInstance().build(RouterPath.MODULE_ADD_PARKING_ACTIVITY).navigation()
        }
        binding.parkingItemRv.offsetChildrenVertical(300)
        //删除车位
        parkingAdapter.setOnItemLongClickListener { adapter, view, position ->
            val id = parkingAdapter.data[position].id
            AlertDialog.Builder(this)
                .setTitle("删除车位提示")
                .setMessage("确定要删除车位吗？ ")
                .setPositiveButton("确定"){dialog,_ ->
                    Toast.makeText(this, "已删除", Toast.LENGTH_SHORT).show()
                    vm.Parking_delete(id)
                    dialog.dismiss()
                }
                .setNegativeButton("取消"){dialog,_ ->
                    dialog.dismiss()
                }
                .create()
                .show()
            return@setOnItemLongClickListener false
        }
        //查询
        binding.parkingEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                parkingAdapter.data.clear()
                val tt =s.toString().trim()
                val filterList = parkingData.filter {
                    it.spaceNumber.contains(tt, ignoreCase = true)
                }
                parkingAdapter.data.addAll(filterList)
                parkingAdapter.notifyDataSetChanged()
            }

        })
        //返回
        binding.parkingBack.setOnClickListener {
            finish()
        }
    }

    override fun loadData() {
        vm.getParkingSuccess.observe(this){
            parkingAdapter.data.clear()
            parkingAdapter.data.addAll(it.reversed())
            parkingData.addAll(parkingAdapter.data)
            parkingAdapter.notifyDataSetChanged()
        }
        vm.getDeleteParkingSuccess.observe(this){
            vm.ParkingManage(2)
            parkingAdapter.notifyDataSetChanged()
        }
    }

    override fun observeDataChange() {

    }

    override fun getLayoutId(): Int = R.layout.activity_parking
    override fun onResume() {
        super.onResume()
        vm.ParkingManage(2)
    }
}